package com.jy.shenghuilogistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import com.jy.shenghuilogistics.bean.Document;
import com.jy.shenghuilogistics.service.DocumentService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController()
@CrossOrigin
public class DocumentController {
    @Resource
    DocumentService documentService;
    //运送中订单数
    @RequestMapping("inTransitOrderNum")
    public String inTransitOrderNum(int empId){
        return String.valueOf(documentService.inTransitOrderNum(empId));
    }
    //总订单数
    @RequestMapping("allOrderNum")
    public String allOrderNum(int empId){
        return String.valueOf(documentService.allOrderNum(empId));
    }
    //已完成订单数
    @RequestMapping("offTheStocksOrderNum")
    public String offTheStocksOrderNum(int empId){
        return String.valueOf(documentService.offTheStocksOrderNum(empId));
    }
    /*通过条件查询数据*/
    @RequestMapping("selAllDocByCondition")
    public List<Document> selAllDocByCondition(Document doc){
        if(doc.getBeginTime() !=null || doc.getBeginTime() != "" && doc.getEndTime() !=null || doc.getEndTime() != ""){
            doc.setBeginTime(doc.getBeginTime().substring(0,10));
            doc.setEndTime(doc.getEndTime().substring(0,10));
        }
        return documentService.selAllDocByCondition(doc);
    }
    /*查询近大于30天数据*/
    @RequestMapping("selDocumentGT30")
    public List<Document> selDocumentGT30(Integer id){
        return documentService.selDocumentGT30(id);
    }
    /*查询近30天数据*/
    @RequestMapping("selDocumentD30")
    public List<Document> selDocumentD30(Integer id){
        return documentService.selDocumentD30(id);
    }
    //获取对应站点的所有运单
    @RequestMapping("getAllDocBySiteName")
    public List<Document> getAllDocBySiteName(String siteName) {
        return documentService.getAllDocBySiteName(siteName);
    }

    //根据id查询运单
    @RequestMapping("getDocumentById")
    public Document getDocumentById(int id) {
        return documentService.getDocumentById(id);
    }
    //添加运单
    @PostMapping("addDocument")
    public String addDocument(@RequestBody Document d) {
        String results = "";
        try{
            results = documentService.addDocument(d);
        }catch (Exception e){
            e.printStackTrace();
            results = "运单添加失败,请重试";
        }
        return results;
    }
    //对应站点最近7日单量
    @RequestMapping("selSevenDayDocBySite")
    public List<Integer> selSevenDayDocBySite(String siteName) {
        return documentService.selSevenDayDocBySite(siteName);
    }
    //修改运单功能
    @RequestMapping("updDoc")
    public String updDoc(Document d) {
        return documentService.updDoc(d);
    }
    //模糊查询所有运单
    @RequestMapping("getAllDoc")
    public List<Document> getAllDoc(Document d) {
        return documentService.getAllDoc(d);
    }
    //运单修改
    @RequestMapping("updateDocument")
    public String updateDocument(Document d) {
        return documentService.updateDocu(d);
    }
    //批量运单接收
    @RequestMapping("listDocReceive")
    public String listDocReceive(@RequestBody Map params) {
        List<Document> documents = (List<Document>) params.get("documents");
        List<Document> ds = new ArrayList<>();
        for(Object o : documents) {
            String jsonObject= JSON.toJSONString(o);
            ds.add(JSONObject.parseObject(jsonObject,Document.class));
        }
        return documentService.listDocReceive(params.get("siteName").toString(),ds);
    }
}
